import http from '@/utils/request'

//  获取分发文件
const getFileList = (data, config) => {
    let url = `/sendfile/media/manage/page`
    return http.get(url, {params: data}, config)
}

// 快捷列表
const getQuickOperateList = (data, config) => {
    let url = `/sendfile/media/quickly/page`
    return http.get(url, {params: data}, config)
}

export const getFileListByTerminalId = (terminalId=0) => {
    return new Promise(async (resolve, reject) => {
        const getQuickParams = {
            quicklyName: '',
            page: 1,
            pageSize: 200000000
        }

        const quickList = await getQuickOperateList(getQuickParams, {})
        const getFileParams = {
            mediaType: '',
            page: 1,
            pageSize: 200000000
        }

        const filterQuickList = quickList.data.records.filter(item => item.client_id.indexOf(terminalId) !== -1)

        console.log('filterQuickList ---', filterQuickList)
        // 获取素材id
        let fileIdTmpList = []
        filterQuickList.forEach(item => {
            if(item.media_id.indexOf(',') !== -1) {
                fileIdTmpList = [...fileIdTmpList, ...item.media_id.split(',')]
            } else {
                fileIdTmpList.push(item.media_id)
            }
        })
        const fileAllList = await getFileList(getFileParams, {})
        let fileList = []
        fileIdTmpList.forEach((item) => {
            const fileIndex = fileAllList.data.records.findIndex((fileItem) => {
                return  fileItem.id == item
            })
            if(fileIndex !== -1) {
                fileList.push(fileAllList.data.records[fileIndex])
            }
        })
        if(terminalId > 0) {
            resolve(fileList)
        } else {
            reject('请传入正确的id')
        }
    })
}
